package gui;

import javax.swing.*;

import player.clientToServer;

public class popupHandler{
	
	private static volatile popupHandler instance = null;
	
	public static popupHandler getInstance() {
        if (instance == null){
            synchronized (popupHandler.class){
                    if (instance == null){
                        instance = new popupHandler();
                    }
        }}
        return instance;
    }
	
	
	public popupHandler(){}
	
	public void getPopup(String type){
		String question = null;
		Object[] options = new String[] { "Yes", "No" };
		JOptionPane pop = new JOptionPane(question);
		pop.setOptions(options);
		JDialog dialog = null;
		
		switch(type){
		
		case "forfiet":
			question = "Opponent has forfieted the game.\n Would you like to find another match?";
			dialog = pop.createDialog(new JFrame(), question);
			dialog.setVisible(true);
			boolean temp1 = getAnswer(pop);
			break;
		
		case "youLost":
			question = "No matter. Better luck next time.\nRematch?";
			dialog = pop.createDialog(new JFrame(), question);
			dialog.setVisible(true);
			boolean temp2 = getAnswer(pop);
			if(!temp2){
				question = "Do you want to try to reconnect to the server?";
			}
			break;
			
		case "youWon":
			question = "GREAT JOB! Would you like a rematch?";
			dialog = pop.createDialog(new JFrame(), question);
			dialog.setVisible(true);
			boolean temp3 = getAnswer(pop);
			if(!temp3){
				dialog.setVisible(false);
			}
			break;
			
		case "tryServerRecon":
			question = "Connection problems with opponent. Do you want to try to reconnect to the server?";
			dialog = pop.createDialog(new JFrame(), question);
			boolean temp4 = getAnswer(pop);
			if(!temp4){
				System.exit(0);
			}
			else if(temp4){
				gameGui.getInstance().terminateGame();
				mainGui.getInstance().display();
				mainGui.getInstance().setHostInput(clientToServer.getInstance().getServerHost().toString());
			}
		}
	
	}
	
	public boolean getAnswer(JOptionPane op){
	    
		String ans = op.getValue().toString();
	    if(ans.equals("Yes")){
	    	return true;
	    }
	    else{
	    	return false;
	    }
	    
	}
}
